import Store from '../store';
import { computeRowlenByContent, computeColWidthByContent, computeRowlenArr, getCellTextInfo } from './getRowlen';
import luckysheetConfigsetting from '../controllers/luckysheetConfigsetting';
import { getCurrentSheetIndex } from './api';

export default function rhchInit (rowheight, colwidth, sheetIndex = getCurrentSheetIndex(), init = true) {
    zoomSetting();// Zoom sheet on first load
    // 行高
    let file = Store.luckysheetfile[sheetIndex];
    const config = file.config;
    // 列宽
    if (colwidth != null) {
        file.visibledatacolumn = [];
        file.ch_width = 0;

        let maxColumnlen = 120;
        let colLen = new Map();
        let mergeArray = [];
        for (let c = 0; c < colwidth; c++) {
            let firstcolumnlen = Store.defaultcollen;

            if (config.columnlen != null && config.columnlen[c] != null) {
                firstcolumnlen = config.columnlen[c];
            } else {
                if (file.data[0] != null && file.data[0][c] != null) {
                    if (firstcolumnlen > 300) {
                        firstcolumnlen = 300;
                    } else if (firstcolumnlen < Store.defaultcollen) {
                        firstcolumnlen = Store.defaultcollen;
                    }

                    if (firstcolumnlen != Store.defaultcollen) {
                        if (config.columnlen == null) {
                            config.columnlen = {};
                        }

                        config.columnlen[c] = firstcolumnlen;
                    }
                }
            }

            // if (config.colhidden != null && config.colhidden[c] != null) {
            //     colLen.set(c, 0);
            //     continue;
            // }

            // 自动行高计算
            if (firstcolumnlen === 'auto') {
                firstcolumnlen = computeColWidthByContent(file.data, c, rowheight);
                for (let i = 0; i < file.data.length; i++) {
                    let cell = file.data[i][c];
                    if (cell && cell.mc && cell.mc.cs) mergeArray.push(cell);
                }
            }
            colLen.set(c, firstcolumnlen);

            // if(maxColumnlen < firstcolumnlen + 1){
            //     maxColumnlen = firstcolumnlen + 1;
            // }
        }
        let canvas = $("#luckysheetTableContent").get(0).getContext("2d");
        canvas.textBaseline = 'top'; // textBaseline以top计算
        if (init) {
            mergeArray.forEach((mergeObj) => {
                if (mergeObj && mergeObj.tb != '2') {
                    let rowlenArr = computeRowlenArr(rowheight, mergeObj.mc.c);
                    let cellHeight = rowlenArr[mergeObj.mc.c];
                    let textInfo = getCellTextInfo(mergeObj, canvas, {
                        r: mergeObj.mc.r,
                        c: mergeObj.mc.c,
                        cellHeight: cellHeight
                    });
                    let computeCollen = 0;

                    if (textInfo != null) {
                        computeCollen = textInfo.textWidthAll + 2;
                    }
                    let finalWidth = computeCollen + 15;
                    if (finalWidth < 70) finalWidth = 70;
                    if (file.filter_select && file.filter_select.row && file.filter_select.column && file.filter_select.row.length && file.filter_select.column.length) {
                        if (mergeObj.mc.c <= file.filter_select.column[1] && mergeObj.mc.c >= file.filter_select.column[0] && mergeObj.mc.r <= file.filter_select.row[0] && mergeObj.mc.r + mergeObj.mc.rs >= file.filter_select.row[0]) {
                            finalWidth += 20;
                        }
                    }
                    let allLen = 0;
                    for (let sc = mergeObj.mc.c; sc < mergeObj.mc.c + mergeObj.mc.cs; sc++) {
                        allLen += colLen.get(sc) || 0;
                    }
                    if (allLen <  finalWidth) {
                        let value = finalWidth - allLen;
                        for (let sc = mergeObj.mc.c; sc < mergeObj.mc.c + mergeObj.mc.cs; sc++) {
                            colLen.set(sc, (colLen.get(sc) || 0) + value / mergeObj.mc.cs);
                        }
                    }
                }
            });
        }

        for (let colLenValue of colLen) {
            if (colLenValue[1] === 0 || config.colhidden != null && config.colhidden[colLenValue[0]] != null) {
                file.visibledatacolumn.push(file.ch_width);// 列的临时长度分布
            } else {
                file.ch_width += Math.round((colLenValue[1] + 1) * Store.zoomRatio);
                file.visibledatacolumn.push(file.ch_width);// 列的临时长度分布
            }
        }

        for (let c = 0; c < colwidth; c++) {
            if (config.columnlen != null && config.columnlen[c] === 'auto') {
                config.columnlen[c] = colLen.get(c);
            }
        }
        // file.ch_width += Math.round((firstcolumnlen + 1) * Store.zoomRatio);

        // file.visibledatacolumn.push(file.ch_width);// 列的临时长度分布

        // Store.ch_width += 120;
        file.ch_width += maxColumnlen;
    }
    if (sheetIndex === getCurrentSheetIndex()) {
        Store.visibledatacolumn = file.visibledatacolumn;
        Store.ch_width = file.ch_width;
    }
    if (rowheight != null) {
        file.visibledatarow = [];
        file.rh_height = 0;

        for (let r = 0; r < rowheight; r++) {
            let rowlen = Store.defaultrowlen;
            if (config.rowlen != null && config.rowlen[r] != null) {
                rowlen = config.rowlen[r];
            }

            // 自动行高计算
            if (rowlen === 'auto') {
                rowlen = computeRowlenByContent(file.data, r);
            } else {
                if (file.data[r] && init) {
                    let wordWarp = false;
                    for (let cell of file.data[r]) {
                        if (cell && cell.tb == '2') {
                            wordWarp = true;
                        }
                    }
                    if (wordWarp) {
                        let temp = computeRowlenByContent(file.data, r);
                        if (config.rowlen[r] && config.customHeight && config.customHeight[r]) {
                            if (temp > rowlen) {
                                file.config.rowlen[r] = temp;
                                rowlen = temp;
                            };
                        } else if (temp > Store.defaultrowlen) {
                            file.config.rowlen[r] = temp;
                            rowlen = temp;
                        }
                    }
                }
            }

            if (config.rowhidden != null && config.rowhidden[r] != null) {
                file.visibledatarow.push(file.rh_height);
                continue;
            }
            file.rh_height += Math.round((rowlen + 1) * Store.zoomRatio);

            file.visibledatarow.push(file.rh_height); // 行的临时长度分布
        }

        // 如果增加行和回到顶部按钮隐藏，则减少底部空白区域，但是预留足够空间给单元格下拉按钮
        if (!luckysheetConfigsetting.enableAddRow && !luckysheetConfigsetting.enableAddBackTop) {
            file.rh_height += 12;
        } else {
            file.rh_height += 80;  // 最底部增加空白
        }
    }

    if (sheetIndex === getCurrentSheetIndex()) {
        Store.visibledatarow = file.visibledatarow;
        Store.rh_height = file.rh_height;
    }
}

export function zoomSetting () {
    // zoom
    Store.rowHeaderWidth = luckysheetConfigsetting.rowHeaderWidth * Store.zoomRatio;
    Store.columnHeaderHeight = luckysheetConfigsetting.columnHeaderHeight * Store.zoomRatio;
    $("#luckysheet-rows-h").width((Store.rowHeaderWidth - 1.5));
    $("#luckysheet-cols-h-c").height((Store.columnHeaderHeight - 1.5));
    $("#luckysheet-left-top").css({ width: Store.rowHeaderWidth - 1.5, height: Store.columnHeaderHeight - 1.5 });
}
